package com.frame.database.proxy;

import java.lang.reflect.Method;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import com.frame.database.DataException;
import com.frame.database.session.JdbcSession;

/**
 * @Title: 系统
 * @Description:
 * @Copyright: Copyright (c) 2015
 * @Company:
 * @author 王海锋
 * @CreatedTime:2015-8-26 下午06:09:34
 * @version 1.0
 */

public class JDaoProxy implements MethodInterceptor{

    private String ftg;

    public JDaoProxy(String ftg){
        this.ftg=ftg;
    }

    public Object intercept(Object obj, Method method, Object[] pams, MethodProxy proxy) throws Throwable {
        // TODO Auto-generated method stub
        Object o=null;
        String methodName=method.getName();
        if(methodName.startsWith("query") || methodName.startsWith("update") || methodName.startsWith("delete") || methodName.startsWith("save")
                || methodName.equals("getObject") || methodName.equals("getObject2") || methodName.equals("getBean"))
        {
            try{
                JdbcSession session=(JdbcSession)obj;
                session.setFtg(ftg);
                o=proxy.invokeSuper(obj,pams);
            }
            catch(Exception e){
                throw new DataException(e.getMessage(),e);
            }finally{
                JdbcSession session=(JdbcSession)obj;
                session.closeStatements();
            }
        }else
            o=proxy.invokeSuper(obj,pams);
        return o;
    }
}
